<template>
    <div class="app-container">
        <el-form label-width="80px">
            <el-form-item label="ID">
                <el-input v-model="resource.id" placeholder="id" disabled></el-input>
            </el-form-item>
            <el-form-item label="名称">
                <el-input v-model="resource.name" placeholder="请输入资源名称"></el-input>
            </el-form-item>
            <el-form-item label="URL">
                <el-input v-model="resource.url" placeholder="请输入资源链接"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="resource.remark" placeholder="请输入资源的备注"></el-input>
            </el-form-item>
            <el-form-item label="方法">
                <el-select v-model="resource.method" placeholder="方法选择">
                    <el-option v-for="(k,v) in httpMethodMap" :label="k" :value="v" :key="v" ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="状态">
                <el-select v-model="resource.status" placeholder="状态选择">
                    <el-option v-for="(k,v) in resourceStatusMap" :label="k" :value="v" :key="v"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="组选择">
                <el-select v-model="resource.groupId" placeholder="状态选择">
                    <el-option v-for="(k,v) in resourceGroupMap" :label="k" :value="v" :key="v"></el-option>
                </el-select>
                <a class="add-group-a" @click="toAddNewGroup()">新增组</a>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="edit()">编辑</el-button>
                <el-button @click="back()">返回</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {Message, MessageBox} from 'element-ui'
    import resourceGroupApi from '../../api/resourceGroup'
    import resourceApi from '../../api/resource'
    import global from '../../utils/global'


    export default {
        name: "resource-manager-edit",
        props:['resourceId'],
        data:function () {
            return {
                resource:{
                    id:0,
                    name:'',
                    method:'0',
                    url:'',
                    status:'0',
                    groupId:'',
                    remark:'',
                },
                resourceGroupMap:{},
                resourceStatusMap:global.resourceStatusMap,
                httpMethodMap:global.httpMethodMap
            }
        },
        methods:{
            edit:function () {
                const _this = this;
                resourceApi.edit(_this.resource)
                    .then(function (data) {
                        if (data.success){
                            Message.success('编辑成功')
                            return
                        }
                        Message.error('编辑失败')
                    }).catch(function () {
                        Message.error('编辑失败')
                    })
            },
            back:function () {
                this.$router.back(-1)
            },
            toAddNewGroup:function () {
                this.$router.push("/security/resource-group-manager-add")
            }
        },
        mounted:function () {
            const _this = this;
            //获取所有组
            resourceGroupApi.findAll()
                .then(function (data) {
                    if (data.length > 0){
                        data.forEach(function (group) {
                            //_this.resourceGroupMap[group.id.toString()] = group.name
                            _this.$set(_this.resourceGroupMap,group.id.toString(),group.name);
                        })
                        _this.resource.groupId = data[0].id.toString()
                    }

                })

            //获取被编辑用户信息
            resourceApi.findById(_this.resourceId)
                .then(function (data) {
                    _this.resource = data
                    _this.resource.method = _this.resource.method.toString();
                    _this.resource.status = _this.resource.status.toString();
                    _this.resource.groupId = _this.resource.groupId.toString();

                }).catch(function () {
                    Message.error('加载该资源信息错误')
                })


        }


    }
</script>

<style scoped>
.add-group-a{
    display: inline-block;
    margin-left: 10px;
    color: rgb(64, 158, 255);
}
</style>
